<?php

App::uses('AppController', 'Controller');

class CurrencyExchangeController extends AppController {
    
    public $pageTitle = "Kursna lista";
    
    public $paginate = array(
        'limit' => ITEMS_PER_PAGE
    );
    
    public $uses = array('CurrencyExchange');
    
    public function index() {
        $filter = $this->parseFilterData('CurrencyExchange');
        
        $this->paginate['order'] = "{$filter->sortBy} {$filter->sortDir}";
        $curreny_exchange = $this->paginate('CurrencyExchange', $filter->conditions);
        
        $this->set('currency_exchange', $curreny_exchange);
        $this->setFilterData($filter);
    }
    
    public function create() {
        $this->set('data', $this->CurrencyExchange->create());
        $this->render('form');
    }
    
    public function edit($id) {
        $this->set('data', $this->CurrencyExchange->findById($id));
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            $this->CurrencyExchange->create($data);
            
            if (! $this->CurrencyExchange->validates()) {
                $this->set('data', $data);
                $this->setFlash('Uneti podaci nisu validni.');
                return $this->render('form');
            }
            
            if (! $this->CurrencyExchange->save($data)) {
                $this->set('data', $data);
                $this->setFlash('Greška pri čuvanju podataka.');
                return $this->render('form');
            }
            
            $this->setFlash('Kurs je uspešno sačuvan.', 'ok');
            $this->redirect('edit/'.$this->CurrencyExchange->id);
        }
        
        $this->redirect('index');
    }
    
    public function remove($id) {
        if ($this->CurrencyExchange->delete($id)) {
            $this->setFlash('Kurs je obrisan.', 'ok');
        } else {
            $this->setFlash('Kurs nije obrisan.');
        }
        
        $this->redirect('index');
    }
    
}
